package hero.mps.web.sys_role.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import hero.mps.utils.ResultUtils;
import hero.mps.utils.ResultVo;
import hero.mps.web.sys_role.entity.AssignParam;
import hero.mps.web.sys_role.entity.AssignVO;
import hero.mps.web.sys_role.entity.RoleParam;
import hero.mps.web.sys_role.entity.SysRole;
import hero.mps.web.sys_role.service.SysRoleService;
import hero.mps.web.sys_role_menu.entity.SysRoleMenuParam;
import hero.mps.web.sys_role_menu.service.SysRoleMenuService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/role")
public class SysRoleController {
    private final SysRoleService sysRoleService;
    private final SysRoleMenuService sysRoleMenuService;

    /**
     * 新增角色
     * @param sysRole
     * @return
     */
    @PostMapping
    public ResultVo addRole(@RequestBody SysRole sysRole){
        LambdaQueryWrapper<SysRole> queryWrapper = Wrappers.lambdaQuery(SysRole.class)
                .eq(SysRole::getRoleType, "2");
        SysRole one = sysRoleService.getOne(queryWrapper);
        if (one != null && sysRole.getRoleType().equals("2")) {
            return ResultUtils.error("该角色类型已存在");
        }
        sysRole.setCreateTime(new Date());
        boolean save = sysRoleService.save(sysRole);
        if (save) {
            return ResultUtils.success("新增成功！");
        }
        return ResultUtils.error("新增失败");
    }

    /**
     * 编辑角色
     * @param sysRole
     * @return
     */
    @PutMapping
    public ResultVo editRole(@RequestBody SysRole sysRole){
        LambdaQueryWrapper<SysRole> queryWrapper = Wrappers.lambdaQuery(SysRole.class)
                .eq(SysRole::getRoleType, "2");
        SysRole one = sysRoleService.getOne(queryWrapper);
        if (one != null && sysRole.getRoleType().equals("2") && !sysRole.getRoleId().equals(one.getRoleId())) {
            return ResultUtils.error("该角色类型已存在");
        }
        sysRole.setUpdateTime(new Date());
        boolean update = sysRoleService.updateById(sysRole);
        if (update) {
            return ResultUtils.success("编辑成功！");
        }
        return ResultUtils.error("编辑失败");
    }

    /**
     * 删除角色
     * @param roleId
     * @return
     */
    @DeleteMapping("/{roleId}")
    public ResultVo deleteRole(@PathVariable("roleId") long roleId){
        boolean delete = sysRoleService.removeById(roleId);
        if (delete) {
            return ResultUtils.success("删除成功！");
        }
        return ResultUtils.error("删除失败");
    }

    /**
     * 获取角色
     * @param roleName
     * @param currentPage
     * @param pageSize
     * @return
     */
    @GetMapping("/list")
    public ResultVo listRole(@RequestParam(required = false) String roleName,
                             @RequestParam(defaultValue = "1") long currentPage,
                             @RequestParam(defaultValue = "10") long pageSize){
        RoleParam roleParam = new RoleParam();
        roleParam.setPageSize(pageSize);
        roleParam.setCurrentPage(currentPage);
        roleParam.setRoleName(roleName);
        IPage<SysRole> list = sysRoleService.list(roleParam);
        return ResultUtils.success("查询成功", list);
    }

    /**
     * 分配权限数据回显
     * @param userId
     * @param roleId
     * @return
     */
    @GetMapping("/getAssignShow")
    public ResultVo getAssignShow(@RequestParam Integer userId, @RequestParam Integer roleId){
        AssignParam assignParam = new AssignParam();
        assignParam.setUserId(userId);
        assignParam.setRoleId(roleId);
        AssignVO show = sysRoleService.getAssignVOShow(assignParam);
        return ResultUtils.success("查询成功", show);
    }

    @PostMapping("/saveRoleMenu")
    public ResultVo saveRoleMenu(@RequestBody SysRoleMenuParam param){
        sysRoleMenuService.saveRoleMenu(param.getRoleId(), param.getMenuIds());
        return ResultUtils.success("分配权限成功");
    }
}
